fix(test): add test environment isolation for worktree and RTK tests#3467
Conversation
- worktree.ts: Added _resetServiceCache() to allow tests to clear cached GitServiceImpl - tests/integration/test-isolation.ts: Shared test isolation utilities - 5 worktree test files: Isolate from global ~/.gsd/preferences.md by resetting service cache and overriding HOME - 2 RTK test files: Clear GSD_RTK_DISABLED before running, restore after These 13 tests pass in CI (clean environment) but fail on developer machines where: - Global preferences set git.main_branch: master but test repos use main - GSD_RTK_DISABLED=1 is set, causing RTK snapshot tests to fail
🟡 PR Risk Report — MEDIUM
Affected Systems
File Breakdown
|
|
Hey @OfficialDelta — heads up that this PR currently has failing CI checks ( 🤖 Automated PR audit — 2026-04-04 |
|
@jeremymcs I checked the windows-portability logs: the build, typecheck, and all test suites actually pass (223 pass, 0 fail in test:packages). The test:unit step shows 0 tests discovered, but that appears to be a pre-existing glob pattern issue on Windows (single-quoted globs in the npm script don't expand in PowerShell), not introduced by this PR. |
|
@jeremymcs I just wanted to follow up with my previous comment. |
trek-e
left a comment
There was a problem hiding this comment.
No linked issue — please open a GitHub issue for these test failures and add 'Closes #NNN' to the PR body.
The isolation approach is correct: resetting HOME + clearing path/service caches in before/after hooks prevents the developer's global preferences from bleeding into tests that use 'main' as the default branch. The GSD_RTK_DISABLED cleanup in the RTK test files is also correct.
One inconsistency: tests/integration/test-isolation.ts provides shared isolateFromGlobalPreferences() and restoreGlobalPreferences() utilities, but none of the test files in this PR actually import or use them — each file duplicates the isolation boilerplate inline. The shared utility has no consumers. Either use it (import and call it in the affected test files) or remove it.
The PR description accurately notes that ~40 integration tests in tests/integration/ have the same issue and are deferred to a follow-up. That is acceptable as long as a follow-up issue is opened.
TL;DR
What: Fixes 13 test failures caused by test environment leaking into assertions.
Why: Tests read ~/.gsd/preferences.md (master vs main mismatch) and inherit GSD_RTK_DISABLED from the environment, causing failures on developer machines that pass in CI.
How: Added _resetServiceCache() for git service isolation, HOME override for preference isolation, and GSD_RTK_DISABLED cleanup in RTK tests.
What
worktree.ts: Added_resetServiceCache()to allow tests to clear cached GitServiceImpltests/integration/test-isolation.ts: Shared test isolation utilitiesWhy
These 13 tests pass in CI (clean environment) but fail on developer machines where:
git.main_branch: masterbut test repos usemainGSD_RTK_DISABLED=1is set, causing RTK snapshot tests to failVerified: all 13 failures are reproducible on current main and resolved by these changes.
How
Test isolation pattern: each affected test suite resets cached services in
beforeEachand restores environment inafterEach. The_resetServiceCache()export is test-only (prefixed with underscore per convention).Note: ~40 integration tests in
tests/integration/have the samegit checkout masterissue. This PR fixes unit tests only — integration test isolation is a follow-up.